'use strict';

const { Service } = require('egg');
const ModelName = 'Resume'
class RouterService extends Service {
    async getPage(where, page, pageSize) {
        return this.ctx.model[ModelName].findAndCountAll({
            where,
            offset: pageSize * (page - 1),
            limit: pageSize,
            order:[
                // id 降序
                ['create_time','DESC']
            ],
        });
    }
    // 所有列表
    async allList(where) {
        return this.ctx.model[ModelName].findAll({
            where,
            order:[
                // id 降序
                ['create_time','DESC']
            ],
        });
    }
    async findOne(id) {
        return this.ctx.model[ModelName].findOne({
            where: {
                id,
            },
        });
    }
    async user(user_id) {
        return this.ctx.model[ModelName].findOne({
            where: {
                user_id,
            },
        });
    }
    async create(data) {
        console.log('data',data)
        // 查找一下根据user_id
        const resume = await this.ctx.model[ModelName].findOne({
            where: { user_id: data.user_id },
        })
        if (resume) {
            // 更新
            this.ctx.model[ModelName].update(data, { where: { user_id: data.user_id } });
            return resume
        } else {
            return this.ctx.model[ModelName].create(data);
        }
    }
    async update(id, data) {
        return await this.ctx.model[ModelName].update(data, { where: { id } });
    }
    async deleteById(id) {
        return await this.ctx.model[ModelName].destroy({ where: { id } });
    }
}

module.exports = RouterService;
